package pascal.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import pascal.excepcion.DAOExcepcion;
import pascal.modelo.Categoria;
import pascal.negocio.GestionCategoria;

/**
 * Servlet implementation class CategoriaServlet
 */
@WebServlet("/CategoriaServlet")
public class CategoriaServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CategoriaServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.processRequest(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.processRequest(request, response);
	}
	
	protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		Object codigoCategoria = request.getParameter("categoriaCode");
		GestionCategoria gestion = new GestionCategoria();
		Collection<Categoria> lista = null;
        
		if(codigoCategoria == null) {
			
			try {
				lista = gestion.listarPadre();
			} catch (DAOExcepcion e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			response.setContentType("text/html;charset=UTF-8");
	        PrintWriter out = response.getWriter();
	        
	        out.println("<option value='-1'>Seleccione categoria</option>");
	        try {
	        	if(lista != null)
	            for (Categoria categoriapadre: lista) {
	                out.printf("<option value='%1s'>%2s</option>", categoriapadre.getCodCategoria(), categoriapadre.getNombre());
	            }
	        } finally {
	            out.close();
	        }
			
		} else {
			
			int codigo = Integer.parseInt((String)codigoCategoria);
			
			if(codigo != -1) {
				
				try {
					lista = gestion.listarHijos(codigo);
				} catch (DAOExcepcion e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				response.setContentType("text/html;charset=UTF-8");
		        PrintWriter out = response.getWriter();
		        
		        try {
		        	if(lista != null)
		            for (Categoria categoriahijo : lista) {
		                out.printf("<option value='%1s'>%2s</option>", categoriahijo.getCodCategoria(), categoriahijo.getNombre());
		            }
		        } finally {
		            out.close();
		        }
				
			} else {
				
				response.setContentType("text/html;charset=UTF-8");
		        PrintWriter out = response.getWriter();
				
				try {
			        
					out.println("<option value='-1'>Seleccione categoria</option>");
				} finally {
					out.close();
				}
				
			}
						
			
		}
		
		
	}

}
